package com.example.hcmiu.englishforkid;

import java.util.Date;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;

public class MainActivity extends Activity {
	public static Activity CurrentActivity;
	public static long lastTime = System.currentTimeMillis();
	public static boolean lock = false;
	public static Runnable r = new Runnable() {

		@Override
		public void run() {
			System.out.println("lock = " + lock + " >> " + new Date());
			final Dialog[] dialog = new Dialog[1];
			while (true) {
				// System.out.println(System.currentTimeMillis());
				long totalTime = lock ? (1000 * 60 * 1) : (1000 * 60 * 2);

				if ((System.currentTimeMillis() - lastTime) > totalTime) {
					lock = !lock;
					System.out.println("lock = " + lock + " >> " + new Date());
					lastTime = System.currentTimeMillis();

					CurrentActivity.runOnUiThread(new Runnable() {

						@Override
						public void run() {

							if (lock) {
								dialog[0] = new Dialog(CurrentActivity, 1) {
									@Override
									protected void onCreate(
											Bundle savedInstanceState) {
										super.onCreate(savedInstanceState);
										setContentView(R.layout.layout_time_for_rest);
										getWindow().setLayout(
												LayoutParams.FILL_PARENT,
												LayoutParams.FILL_PARENT);
									}
								};
							}
							
							if (lock)
								dialog[0].show();
							else
								dialog[0].dismiss();

						}
					});

				}

				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		MainActivity.CurrentActivity = this;

		Thread thread = new Thread(r);
		thread.start();

		Button bt_start = (Button) findViewById(R.id.bt_start);
		bt_start.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(getApplicationContext(),
						MenuStartActivity.class);
				startActivity(intent);
			}
		});

		Button bt_learnvideo = (Button) findViewById(R.id.bt_learnvideo);
		bt_learnvideo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(getApplicationContext(),
						VideoActivity.class);
				startActivity(intent);
			}
		});

		Button bt_play_game_listen_and_guess = (Button) findViewById(R.id.bt_play_game_listen_and_guess);
		bt_play_game_listen_and_guess.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(getApplicationContext(),
						GameListenAndGuessActivity.class);
				startActivity(intent);
			}
		});
		
		Button bt_progress = (Button) findViewById(R.id.bt_progress);
		bt_progress.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(getApplicationContext(),
						ProgressActivity.class);
				startActivity(intent);
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
